<?php
class EmployeeAction extends Action {
    public function employees(){
        
        $employees = new Model('employees');
        $employeesCount = $employees->Count();
        $this->assign('employeesCount', $employeesCount);

        $pageurl = 'Employee/employees';
        $itemsPerPage = 20;
        $pageCount = ceil($employeesCount/$itemsPerPage);
        $maxButtons = 9;
        $curPage = $this->_get('p');
        $page = pagination($pageCount, $itemsPerPage, $pageUrl, $curPage, $maxButtons);
        $this->assign('page', $page);

        $data = $employees->join('inner join dept_emp ON employees.emp_no = dept_emp.emp_no')
                          ->join('inner join departments ON dept_emp.dept_no = departments.dept_no')
                          ->join('inner join titles ON employees.emp_no = titles.emp_no')
                          ->field('employees.emp_no as emp_no, first_name, last_name, gender, hire_date, departments.dept_no as dept_no, departments.dept_name as dept_name, titles.title as title')
                          ->limit(($curPage-1)*$itemsPerPage, $itemsPerPage)->select();
        $this->assign('employees', $data);
        $title = 'Employees | EMS Systems';
        $this->assign('title', $title);
        $this->display();
    }

    public function employee(){
        
        $emp_no = $this->_param('id');
        $employee = M('employees');
        $condition = array('emp_no' => $emp_no);
        $data = $employee->where($condition)->find();
        $this->assign('employee', $data);

        $departments = M('dept_emp');
        $data = $departments->where($condition)
                            ->field('departments.dept_name as dept_name, dept_emp.from_date as from_date, dept_emp.to_date as to_date')
                            ->join('departments on departments.dept_no = dept_emp.dept_no')
                            ->select();
        $this->assign('departments', $data);

        $titles = M('titles');
        $data = $titles->where($condition)->select();
        $this->assign('titles', $data);
        $this->display();
    }
}
